﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Configuration;

namespace HQDBF
{
    public class ETFPriceQuery
    {
        #region Members

        private string connETF;

        readonly string SAVE_PRICE = "INSERT INTO MinPrice(StockCode, Price, RecordTime) VALUES('{0}',{1},'{2}')";
        readonly string GET_PRICE = "SELECT * FROM MinPrice ORDER BY RecordTime DESC limit 0,2000;";

        #endregion

        public ETFPriceQuery()
        {
            connETF = string.Format("Data Source={0};Pooling=true;", ConfigurationManager.AppSettings["ETFPath"]);
        }

        public void SavePrice(List<ETFPrice> list)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connETF))
            {
                conn.Open();
                var trans = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    foreach (var item in list)
                    {
                        SQLiteHelper.ExecuteNonQuery(trans, string.Format(SAVE_PRICE, item.StockCode, item.Price, item.RecordTime.ToString("yyyy-MM-dd hh:mm:ss.fff")));
                    }

                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }

        public Dictionary<string, List<double>> GetPrices(int rowCount)
        {
            Dictionary<string, List<double>> result = new Dictionary<string, List<double>>();

            try
            {
                using (SQLiteConnection conn = new SQLiteConnection(connETF))
                {
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandType = System.Data.CommandType.Text;

                        using (var dr = SQLiteHelper.ExecuteReader(cmd, GET_PRICE, null))
                        {
                            while (dr.Read())
                            {
                                string key = dr["StockCode"].ToString();
                                if (!result.ContainsKey(key))
                                {
                                    result.Add(key, new List<double>());
                                }
                                result[key].Add(Convert.ToDouble(dr["Price"]));
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return result;
        }
    }


    /// <summary>
    /// ETF价格实体
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-24 4:50 PM</datetime>
    /// </remarks>
    public class ETFPrice
    {
        public string StockCode { get; set; }
        public double Price { get; set; }
        public DateTime RecordTime { get; set; }

        public ETFPrice(string code, double price)
        {
            this.StockCode = code;
            this.Price = price;
            this.RecordTime = DateTime.Now;
        }
    }
}
